PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

Count

The Count command can function as an AppleScript command or an application command. The AppleScript command counts the number of elements of a particular class in a list, record, or string. The application command counts the number of elements of a particular class in an object or objects.

APPLESCRIPT COMMAND SYNTAX
count [ [ each | every ] className | pluralClassName ( in | of ) ] compositeValue number of [ className | pluralClassName ( in | of ) ] compositeValue
APPLICATION COMMAND SYNTAX
count [ each | every ] className | pluralClassName [ ( in | of ) referenceToObject ] number of className | pluralClassName [ ( in | of ) referenceToObject ]
PARAMETERS
className
The class name of the elements to be counted. If you use the term each or every , you should use only the singular form of the class name, even though in some cases AppleScript or an application may handle the plural form when it is used incorrectly. The elements of lists, records, and strings are listed in the value class definitions in Values and Constants. The elements of application objects are listed in their object class definitions in the application dictionary. Class: Class identifier Default value. Item for lists, records, and application objects; Character for strings (see "Notes")
pluralClassNam e
The plural class name of the elements to be counted. You should use the plural form of the class name when appropriate, even though in some cases AppleScript or an application may handle the singular form when it is used incorrectly.The elements of lists, records, and strings are listed in the value class definitions in Values and Constants. Class: Class identifier Default value. Item for lists, records, and application objects; Character for strings (see "Notes")
compositeValue
An expression that evaluates to a composite value whose elements are to be counted. Class: List, record, reference, or string
referenceToObject
A reference to the object or objects whose elements are to be counted. If you do not specify this parameter, the application counts the elements in the default target of the Tell statement. Class: List, record, reference, or string
RESULT

The result of the AppleScript command is an integer that specifies the number of elements of a specified class in a composite value.

The result of the application command is either an integer or a list of integers. See "Notes" for details.

Class: Integer or list of integers

EXAMPLES

In the following example, compositeValue is a list. The command does not explicitly specify a class of elements to count, so AppleScript counts all the items in the list.

count {"Yes", "No", "Maybe", 4, 5, 6}
--result: 6

In this example, className is integers and referenceToObject is a list of strings and integers. The following statements count first the integers in the list, then the strings:

count the integers in {"Yes", "No", "Maybe", 4, 5, 6}
--result: 3
count the strings in {"Yes", "No", "Maybe", 4, 5, 6}
--result: 3

This example shows another way to count the integers in the list:

count each integer in {"Yes", "No", "Maybe", 4, 5, 6}
--result: 3

You can use the term number of as well to count items in a list:

number of integers in {"Yes", "No", "Maybe", 4, 5, 6}
--result: 3

In the following example, every file of disk 1 evaluates to a list of files. The Finder counts the files in the list.

tell application "Finder"
    count every file of disk 1
end tell
--result: number of files on first disk

The following statement is equivalent to the previous example:

tell application "Finder"
    count files of disk 1
end tell

In the following example, referenceToObject is windows of application "Finder", which is a list of windows. The Finder counts the windows in the list.

count of windows of application "Finder"
NOTES

If you use the Count command on a string without specifying the class to be counted, AppleScript counts the characters. Consider the following two examples.

count "This is a string"
--result: 16
count words in "This is a string"
--result: 4

If you count a list or record without specifying a class, the Count command counts the items:

count {1, 2, 5, 3} --result: 4
count {name: Jun, height: 72, weight: 200} --result: 3

© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)